import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { FileService } from '@core';
import { SFSchema, SFUISchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { environment } from '@env/environment';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';

@Component({
    selector: 'app-sys-user',
    templateUrl: './execl-import.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ExeclImportComponent implements OnInit {
    constructor(
        private http: _HttpClient, 
        private msgSrv: NzMessageService,
        private modal: NzModalRef,
        private cdr: ChangeDetectorRef,
        private fileService: FileService
        ) { }

    isSpinning = false;
    spinningMessage = '';

    ngOnInit(): void {
        this.ui = {
            $file: {
                text: '点击上传导入',
                accept: ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
                widget: 'upload',
                action: `${environment.sys.ctx}/execl/import/${this.params.id}`,
                resReName: 'resource_id',
                urlReName: 'url',
                data: this.params,
                showUploadList: false,
                customRequest: (item: any) => {
                    this.isSpinning = true;
                    this.spinningMessage = '导入中···';
                    const form = new FormData()
                    form.append('file', item.file, item.file.name)
                    return this.http.post(item.action, form, item.data).subscribe(res => {
                        if (res.success === false) {
                            this.msgSrv.error(res.message);
                            this.spinningMessage = res.message;
                            const a = document.createElement('a');
                            a.href = `data:application/octet-stream;base64,${res.data}`;
                            a.download = decodeURIComponent(item.file.name);
                            a.click();
                        } else {
                            this.spinningMessage = '导入成功！';
                            this.msgSrv.success('导入成功！');
                        }
                        this.cdr.detectChanges();
                    },res=>{},()=>{
                        this.isSpinning = false;
                        this.cdr.detectChanges();
                    })
                }
            }
        };
        this.isLoadComplete = true
    }
    isLoadComplete = false;
    params: any = {}
    schema: SFSchema = {
        properties: {
            tempalte: {
                type: 'string', title: '模板', ui: {
                    widget: 'custom',
                }
            },
            file: { type: 'string', title: '文件' },
        }
    };
    ui: SFUISchema = {};

    downloadTemplate() {
        this.fileService.download(`${environment.sys.ctx}/execl/download/template/${this.params.id}?name=${this.params.name}`)
    }

    close() {
        this.modal.destroy();
      }
}